package com.yc.cloud.portal.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.yc.cloud.basic.api.ResponseResult;
import com.yc.cloud.basic.config.PathUtils;
import com.yc.cloud.basic.utils.MyFileConvertUtils;
import com.yc.cloud.basic.utils.MyFileUtils;
import com.yc.cloud.openai.dto.IATResultDTO;
import com.yc.cloud.openai.dto.TTSResultDTO;
import com.yc.cloud.openai.request.IatParam;
import com.yc.cloud.openai.request.TTSParam;
import com.yc.cloud.openai.service.IATService;
import com.yc.cloud.openai.service.tts.TTSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.val;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Paths;

@RestController
@RequestMapping("ai/voice")
@Api(tags = "AI-语音模块")
public class VoiceController {

    private final TTSService xfTts;
    private final TTSService hfTts;
    private final IATService xfIatService;
    private final TTSService chatTts;

    public VoiceController(@Qualifier("XFTTSServiceImpl") TTSService ttsService, @Qualifier("HFKJTTSServiceImpl") TTSService hfTts, @Qualifier("XFIatServiceImpl") IATService xfIatService, @Qualifier("ChatTTSServiceImpl") TTSService chatTts) {
        this.xfTts = ttsService;
        this.hfTts = hfTts;
        this.xfIatService = xfIatService;
        this.chatTts = chatTts;
    }

    @PostMapping("xf/tts")
    @ApiOperation(value = "科大讯飞-文本转语音", notes = "将文本内容转成音频文件,支持wav,pc格式,音色支持男声和女声")
    public ResponseResult<TTSResultDTO> xfTts(@Validated TTSParam param) {
        val result = xfTts.getTTSResult(param);
        if (StrUtil.isNotEmpty(result.getResult())) {
            val pcmPath = Paths.get(PathUtils.getTempBasePath(), System.currentTimeMillis() + "-xf-tts.pcm").toString();
            MyFileUtils.decodeAndSaveFile(result.getResult(), pcmPath);
            var dstPath = pcmPath;
            if ("wav".equals(param.getFormat())) {
                val wavPath = pcmPath.replace(".pcm", ".wav");
                MyFileConvertUtils.pcm2wav(pcmPath, wavPath);
                dstPath = wavPath;
            }
            // MyFileUtils.convertToBase64(FileUtil.file(wavPath));
            result.setUrl(PathUtils.convertNetUrl(dstPath));
        }
        return ResponseResult.success(result);
    }


    @SneakyThrows
    @PostMapping("xf/iat")
    @ApiOperation(value = "科大讯飞-语音识别", notes = "语音文件内容提出成文字信息")
    public ResponseResult<IATResultDTO> xfIat(@Validated IatParam param, @RequestPart MultipartFile file) {
        //文件内容转成base64
        val dstPath = PathUtils.getTempBasePath() + File.separator + System.currentTimeMillis() + "-xf-iat." + file.getOriginalFilename().split("\\.")[1];
        FileUtil.writeFromStream(file.getInputStream(), dstPath);
        param.setContent(dstPath);
        val result = xfIatService.getIatResult(param);
        return ResponseResult.success(result);
    }


    @PostMapping("hf/tts")
    @ApiOperation(value = "HF-文本转语音", notes = "")
    public ResponseResult<TTSResultDTO> hfTts(@Validated TTSParam param) {
        return ResponseResult.success(hfTts.getTTSResult(param));
    }

    @PostMapping("chat/tts")
    @ApiOperation(value = "CHAT-文本转语音", notes = "")
    public ResponseResult<TTSResultDTO> chatTTS(@RequestBody @Validated TTSParam param) {
        return ResponseResult.success(chatTts.getTTSResult(param));
    }
}
